﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using VAdvantage.DSProcessWorkflow;
using System.Xml.Linq;
using Location;
using VAdvantage.CountryService;
using VAdvantage.GlobalWeatherService;
using VAdvantage.Model;
using VAdvantage.Utility;

namespace VAdvantage.Widgets
{
    public partial class Settings : ChildWindow
    {
        #region Global Declarations

        //countrySoapClient _CountrySoapClient;
        //GlobalWeatherSoapClient _GlobalWeatherSoapClient;
        //DSProcessWorkflowClient _SettingsServiceClient;

        #endregion

        #region Public Properties

        private string _RootPath;
        public string RootPath
        {
            get { return _RootPath; }
            set { _RootPath = value; }
        }

        #endregion

        #region Constructors

        public Settings(string rootPath)
        {
            RootPath = rootPath;

            InitializeComponent();

          // new countrySoapClient();
           // _GlobalWeatherSoapClient = AEnv.GetGlobalWeatherSoapClient();// new GlobalWeatherSoapClient();
           // _SettingsServiceClient = AEnv.GetDSPWFClient();// new DSProcessWorkflowClient();


            var _CountrySoapClient = AEnv.GetcountrySoapClient();
            _CountrySoapClient.GetCountriesCompleted += new EventHandler<GetCountriesCompletedEventArgs>(GetCountriesCompleted);
            _CountrySoapClient.GetCountriesAsync();
            _CountrySoapClient.CloseAsync();

            //busyIndicator.IsBusy = true;
        }

        #endregion

        #region Control Events

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (cmbCity.SelectedItem != null)
            {
                City selectedCity = (City)cmbCity.SelectedItem;

                var _SettingsServiceClient = AEnv.GetDSPWFClient();
                _SettingsServiceClient.SerializeToXMLCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(SerializeToXMLCompleted);
                _SettingsServiceClient.SerializeToXMLAsync(selectedCity.CountryName, selectedCity.CityName, (Dictionary<String, String>)Envs.GetCtx().GetMap());
                _SettingsServiceClient.CloseAsync();
                this.OKButton.IsEnabled = false;
                this.Cursor = Cursors.Wait;
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void cmbCountry_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cmbCountry.SelectedItem != null)
            {
                Country selectedCountry = (Country)cmbCountry.SelectedItem;
                var _GlobalWeatherSoapClient = AEnv.GetGlobalWeatherSoapClient();
                _GlobalWeatherSoapClient.GetCitiesByCountryCompleted += new EventHandler<GetCitiesByCountryCompletedEventArgs>(GetCitiesByCountryCompleted);
                _GlobalWeatherSoapClient.GetCitiesByCountryAsync(selectedCountry.Name);
                _GlobalWeatherSoapClient.CloseAsync();

            }
            //busyIndicator.IsBusy = true;
        }

        #endregion

        #region Service Generated Events

        void GetCountriesCompleted(object sender, GetCountriesCompletedEventArgs e)
        {
            try
            {
                string strCountries = e.Result;
                if (!string.IsNullOrEmpty(strCountries))
                {
                    List<Country> lstCountries = new List<Country>();
                    XDocument xDoc = XDocument.Parse(strCountries);
                    var countries = xDoc.Root.Descendants("Table");
                    foreach (var country in countries)
                    {
                        lstCountries.Add(new Country { Name = country.Descendants("Name").First().Value });
                    }
                    cmbCountry.DisplayMemberPath = "Name";
                    cmbCountry.ItemsSource = lstCountries;
                }
            }
            catch
            {
            }

            //busyIndicator.IsBusy = false;
        }

        void GetCitiesByCountryCompleted(object sender, GetCitiesByCountryCompletedEventArgs e)
        {
            try
            {
                string strCities = e.Result;
                if (!string.IsNullOrEmpty(strCities))
                {
                    List<City> lstCities = new List<City>();
                    XDocument xDoc = XDocument.Parse(strCities);
                    var cities = xDoc.Root.Descendants("Table");
                    foreach (var city in cities)
                    {
                        City newCity = new City();
                        newCity.CountryName = city.Descendants("Country").First().Value;
                        newCity.CityName = city.Descendants("City").First().Value;

                        lstCities.Add(newCity);
                    }
                    if (lstCities.Count > 0)
                    {
                        List<City> lstSortedCities = lstCities.OrderBy(c => c.CityName).ToList();
                        cmbCity.DisplayMemberPath = "CityName";
                        cmbCity.ItemsSource = lstSortedCities;
                    }
                }
            }
            catch
            {

            }
            //busyIndicator.IsBusy = false;
        }

        void SerializeToXMLCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            this.OKButton.IsEnabled = true;
            this.Cursor = Cursors.Arrow;
            this.DialogResult = true;
        }

        #endregion
    }
}

